package com.xing.service.impl;

import com.xing.service.AsyncTaskService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.Future;

/**
 * Created by Bradish7Y on 2017/8/25.
 */
@Service
public class AsyncTaskServiceImpl implements AsyncTaskService {

    @Override
    @Async
    public Future<String> task1() throws Exception {
        Random random = new Random();
        System.out.println("开始做任务一");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务一，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<String>("task1");
    }

    @Override
    @Async
    public Future<String> task2() throws Exception {
        Random random = new Random();
        System.out.println("开始做任务二");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务二，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<String>("task2");
    }

    @Override
    @Async
    public Future<String> task3() throws Exception {
        Random random = new Random();
        System.out.println("开始做任务三");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务三，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<String>("task3");
    }
}
